package com.zhuoyue.web.controller.system;

import com.zhuoyue.common.core.domain.AjaxResult;
import com.zhuoyue.system.domain.SysImage;
import com.zhuoyue.system.domain.vo.FileUploadResult;
import com.zhuoyue.system.mapper.TblDeviceZYCategoryMapper;
import com.zhuoyue.system.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author zhanghui
 * @date 2021/10/19 10:01
 */
@RestController
@RequestMapping("/system/image")
public class ImageController {
    @Autowired
    private FileUploadService fileUploadService;

    @Autowired
    private TblDeviceZYCategoryMapper categoryMapper;

    /**
     * 上传系统动态图片
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadMovementImage")
    public AjaxResult uploadMovementImage(MultipartFile file) throws IOException {
        SysImage sysImage = new SysImage();
        sysImage.setId(UUID.randomUUID().toString().replace("-", "").toLowerCase());
        // 上传文件
        String originalName = file.getOriginalFilename();
        FileUploadResult upload = this.fileUploadService.upload(file, getMovementImageFilePath(sysImage.getId() + getSuffixName(originalName)));

        sysImage.setImageUid(upload.getUid());
        sysImage.setUrl("https://www.img.zyzncloud.com/" + upload.getUid());
        sysImage.setOssUrl("https://zhuoyuezn-img.oss-cn-hangzhou.aliyuncs.com/" + upload.getUid());
        return AjaxResult.success(sysImage);
    }

    /**
     * 上传动态外链页面图片
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadMovementLinkImage")
    public AjaxResult uploadMovementLinkImage(MultipartFile file) {
        SysImage sysImage = new SysImage();
        sysImage.setId(UUID.randomUUID().toString().replace("-", "").toLowerCase());
        // 上传文件
        String originalName = file.getOriginalFilename();
        FileUploadResult upload = this.fileUploadService.upload(file, getMovementLinkImageFilePath(sysImage.getId() + getSuffixName(originalName)));

        sysImage.setImageUid(upload.getUid());
        sysImage.setUrl("https://www.img.zyzncloud.com/" + upload.getUid());
        sysImage.setOssUrl("https://zhuoyuezn-img.oss-cn-hangzhou.aliyuncs.com/" + upload.getUid());
        return AjaxResult.success(sysImage);
    }

    /**
     * 上传组织头像图片
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadDeptLogoImage")
    public AjaxResult uploadLogoImage(MultipartFile file) throws IOException {
        SysImage sysImage = new SysImage();
        // 设置uuid和文件大小
        sysImage.setId(UUID.randomUUID().toString().replace("-", "").toLowerCase());
        sysImage.setSize(file.getSize());

        // 获取图片的宽高
        BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
        if (bufferedImage != null) {
            sysImage.setWidth(bufferedImage.getWidth());
            sysImage.setHeight(bufferedImage.getHeight());
        }

        // 上传文件
        String originalName = file.getOriginalFilename();
        FileUploadResult upload = this.fileUploadService.upload(file, getDeptLogoImagePath(sysImage.getId() + getSuffixName(originalName)));

        sysImage.setImageUid(upload.getUid());
        sysImage.setUrl("https://www.img.zyzncloud.com/" + upload.getUid());
        sysImage.setOssUrl("https://zhuoyuezn-img.oss-cn-hangzhou.aliyuncs.com/" + upload.getUid());

        return AjaxResult.success(sysImage);
    }

    /**
     * 上传产品图片
     * 展示图uuid与主键ID一致, 入网图加后缀"_1"
     *
     * @param file
     * @param categoryId
     * @return
     */
    @PostMapping("/uploadProductInfoImage")
    public AjaxResult uploadProductInfoImage(MultipartFile file, Long categoryId, String uuid) throws IOException {
        SysImage sysImage = new SysImage();
        // 设置uuid和文件大小
        sysImage.setId(uuid);
        sysImage.setSize(file.getSize());

        // 获取图片的宽高
        BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
        if (bufferedImage != null) {
            sysImage.setWidth(bufferedImage.getWidth());
            sysImage.setHeight(bufferedImage.getHeight());
        }

        // 根据传入的类型构建图片路径
        List<String> categoryNumList = categoryMapper.getPathByCategoryId(categoryId);
        StringBuilder categoryPath = new StringBuilder();
        for (String categoryNum : categoryNumList) {
            categoryPath.append("/").append(categoryNum);
        }
        categoryPath = new StringBuilder(categoryPath.length() > 1 ? categoryPath.substring(1) : "all");

        // 上传文件
        String originalName = file.getOriginalFilename();
        FileUploadResult upload = this.fileUploadService.upload(file, getProductInfoImagePath(sysImage.getId() + getSuffixName(originalName), categoryPath.toString()));

        sysImage.setImageUid(upload.getUid());
        sysImage.setUrl("https://www.img.zyzncloud.com/" + upload.getUid());
        sysImage.setOssUrl("https://zhuoyuezn-img.oss-cn-hangzhou.aliyuncs.com/" + upload.getUid());

        return AjaxResult.success(sysImage);
    }

    @PostMapping("/uploadImage")
    public AjaxResult uploadProductInfoImage(MultipartFile file, String path, String fileName) throws IOException {
        SysImage sysImage = new SysImage();
        sysImage.setSize(file.getSize());
        // 没有指定目录则为默认目录
        if (path == null) {
            path = "zyzn/defaultDirectory/";
        }
        // 没有指定文件名则生成uuid
        if (fileName == null) {
            fileName = UUID.randomUUID().toString().replace("-", "").toLowerCase();
            sysImage.setId(fileName);
            fileName += getSuffixName(file.getOriginalFilename());
        }

        // 获取图片的宽高
        BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
        if (bufferedImage != null) {
            sysImage.setWidth(bufferedImage.getWidth());
            sysImage.setHeight(bufferedImage.getHeight());
        }

        FileUploadResult upload = this.fileUploadService.upload(file, path + fileName);
        sysImage.setImageUid(upload.getUid());
        sysImage.setUrl("https://www.img.zyzncloud.com/" + upload.getUid());
        sysImage.setOssUrl("https://zhuoyuezn-img.oss-cn-hangzhou.aliyuncs.com/" + upload.getUid());
        return AjaxResult.success(sysImage);
    }

    // oss上的文件路径
    private String getProductInfoImagePath(String sourceFileName, String categoryPath) {
        return "zyzn/productImage/" + categoryPath + "/" + sourceFileName;
    }

    private String getDeptLogoImagePath(String sourceFileName) {
        return "zyzn/logoImage/" + sourceFileName;
    }

    private String getMovementImageFilePath(String sourceFileName) {
        return "zyzn/movementImage/" + sourceFileName;
    }

    // 动态点击后的内容页的图片
    private String getMovementLinkImageFilePath(String sourceFileName) {
        return "zyzn/movementLinkImage/" + sourceFileName;
    }

    /**
     * 获取文件扩展名, 如'.png'
     *
     * @param originalName 文件原本的名称, 如'1.png'
     * @return 文件扩展名, 如'.png', 若没有则返回空字符串
     */
    private String getSuffixName(String originalName) {
        String suffixName = "";
        if (originalName != null && originalName.lastIndexOf('.') > 0) {
            suffixName += originalName.substring(originalName.lastIndexOf('.'));
        }

        return suffixName;
    }
}
